package com.software.graduationproject.modules.order.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.Page;


import com.software.graduationproject.commons.select.Select;
import com.software.graduationproject.commons.util.DateUtils;
import com.software.graduationproject.modules.food.mapper.FoodMapper;
import com.software.graduationproject.modules.food.object.FoodVo;
import com.software.graduationproject.modules.marketers.mapper.MarketersMapper;
import com.software.graduationproject.modules.marketers.object.MarketersVo;
import com.software.graduationproject.modules.order.entity.OrderInfo;
import com.software.graduationproject.modules.order.mapper.OrderInfoMapper;
import com.software.graduationproject.modules.order.object.OrderInfoVo;
import com.software.graduationproject.modules.order.object.OrderInfoCreateQo;
import com.software.graduationproject.modules.order.object.OrderInfoUpdateQo;

import com.software.graduationproject.modules.shopping.entity.ShoppingCart;
import com.software.graduationproject.modules.shopping.mapper.ShoppingCartMapper;
import com.software.graduationproject.modules.shopping.object.ShoppingCartVo;
import com.software.graduationproject.modules.shopping.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

import static com.software.graduationproject.commons.util.DateUtils.TIME_FORMAT;

/**
 * OrderInfoService : 订单表
 * ---------------------------------------------------------------------------------------------------------------------
 */
@Service
public class OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private MarketersMapper marketersMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    public void createQo(List<OrderInfoCreateQo> qos){
        for (OrderInfoCreateQo qo : qos) {
            Long shoppingCartId = qo.getShoppingCartId();
            ShoppingCart shoppingCart = shoppingCartMapper.selectById(shoppingCartId);
            if (shoppingCart != null) {
                shoppingCart.setStatus(1);
                shoppingCartMapper.updateById(shoppingCart);
            }
            FoodVo foodVo = shoppingCartMapper.selectFoodByCartId(shoppingCartId);
            if (foodVo != null) {
                foodVo.setSales(foodVo.getSales() + qo.getQuantity());
                foodMapper.updateById(foodVo);
            }
            MarketersVo marketersVo = shoppingCartMapper.selectMarketersByCartId(shoppingCartId);
            if (marketersVo != null) {
                marketersVo.setSales(marketersVo.getSales() + 1);
                marketersMapper.updateById(marketersVo);
            }
            OrderInfo orderInfo = new OrderInfo();
            BeanUtil.copyProperties(qo, orderInfo);
            orderInfoMapper.insert(orderInfo);
        }
    }
//
//    public void updateQo(OrderInfoUpdateQo qo){
//        OrderInfo orderInfo = orderInfoMapper.select(qo.getId());
//        if (orderInfo == null){
//            throw  new ServiceException("数据不存在");
//        }
//
//        Beans.copyPropertiesNotNull(orderInfo, qo);
//        orderInfoMapper.update(orderInfo);
//    }
//
//    public OrderInfoVo selectVo(Long id) {
//        OrderInfoVo orderInfoVo = new OrderInfoVo();
//        OrderInfo orderInfo = orderInfoMapper.select(id);
//        Beans.copyPropertiesNotNull(orderInfoVo, orderInfo);
//        return orderInfoVo;
//    }

    public void delete(Long []ids) {
        LambdaQueryWrapper<OrderInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(OrderInfo::getId,ids);
        orderInfoMapper.delete(queryWrapper);
    }

    public Page<OrderInfoVo> selectPageVo(Select select){
        return orderInfoMapper.selectPageVo(select);
    }

    public List<OrderInfoVo> selectListVo(Select select){
        List<OrderInfoVo> orderInfoVos = orderInfoMapper.selectListVo(select);
        Map<AbstractMap.SimpleEntry<Long, Date>, List<OrderInfoVo>> groupedByMarketersId = orderInfoVos.stream()
                .collect(Collectors.groupingBy(
                        orderInfoVo -> new AbstractMap.SimpleEntry<>(orderInfoVo.getMarketersId(), orderInfoVo.getCreatedDate())
                ));
        List<OrderInfoVo> result = new ArrayList<>();
        groupedByMarketersId.forEach((marketersId, carts) -> {
            OrderInfoVo originalCart = carts.get(0);
            List<ShoppingCartVo.Foods> foodList = new ArrayList<>();
            for (OrderInfoVo cart : carts) {
                ShoppingCartVo.Foods food = new ShoppingCartVo.Foods();
                food.setId(cart.getId());
                food.setFoodName(cart.getFoodName());
                food.setFoodImg(cart.getFoodImg());
                food.setQuantity(cart.getQuantity());
                food.setPrice(cart.getPrice());
                food.setOriginalPrice(cart.getOriginalPrice());
                food.setTaste(cart.getTaste());
                food.setStatus(cart.getStatus());
                foodList.add(food);
            }
            OrderInfoVo vo = new OrderInfoVo();
            BeanUtil.copyProperties(originalCart,vo);
            vo.setFoodList(foodList);
            Date createdDate = vo.getCreatedDate();
            String createdTime = DateUtil.format(createdDate, TIME_FORMAT);
            vo.setCreateTime(createdTime);
            result.add(vo);
        });
        List<OrderInfoVo> collect = result.stream()
                .sorted(Comparator.comparing(OrderInfoVo::getStatus).reversed()
                        .thenComparing(OrderInfoVo::getCreatedDate).reversed())
                .collect(Collectors.toList());
        return collect;
    }
}
